<?php
namespace DaChongQing\Controller;

use AiChongWu\Controller\Wechat;
use Think\Controller;

Class WeChatSdkController extends Controller
{
    public function ReplyMsg($text = '' , $openid ,$from){
        $FuncFlag = $this->_funcflag ? 1 : 0;
        $msg = array(
            'ToUserName' => $openid,
            'FromUserName'=>$from,
            'MsgType'=>'text',
            'Content'=>$this->$text,
            'CreateTime'=>time(),
            'FuncFlag'=>$FuncFlag
        );
        return $msg;
    }
    public function index()
    {
        $options = array(
            'token' => C("TOKEN"),
            'appid' => C("APPID"),
            'appsecret' => C("APPSECRET"),

        );
        $wechat = new Wechat($options);
//        $wechat->valid();//明文或兼容模式可以在接口验证通过后注释此句，但加密模式一定不能注释，否则会验证失败
        $type = $wechat->getRev()->getRevType();
        /**
         * 判断返回事件，做出相应回应
         */
        switch ($type) {
            case Wechat::MSGTYPE_TEXT:
                $openid = $wechat->getRevFrom();
                $info = M('user_info')->where(array('openid'=>$openid))->find();
                $str = "亲，小渝收到了！";
                $wechat->transfer_customer_service('anqiang@woaidachongqing')->reply();
                $wechat->text($str)->reply();
                break;

            case Wechat::MSGTYPE_EVENT:
                $EventType = $wechat->getRev()->getRevEvent();
                switch ($EventType['event']) {
                    case Wechat::EVENT_LOCATION:
                        $location = $wechat->getRev()->getRevEventGeo();
                        $openid = $wechat->getRevFrom();
                        $result = GetLocation($location['x'],$location['y']);
                        $city = $result->city;
                        M('user_info')->where(array('openid'=>$openid))->save(array('location'=>$city));
//                        $location = json_encode($location);
//                        $wechat->text($location)->reply();
                        break;
//                    case Wechat::EVENT_SCAN:
//                        $qrcode = $wechat->getRev()->getRevSceneId();
//                        $wechat->text($qrcode)->reply();
//                        break;
                    //这里写关注事件的逻辑
                    case Wechat::EVENT_SUBSCRIBE:
                        $openid = $wechat->getRevFrom();
                        $info = json_decode(GetUserInfo($openid));
                        $qrcode = $wechat->getRev()->getRevSceneId();
                        if ($qrcode != '') {
                            M('user_qrcode')->where(array('qid' => $qrcode))->setInc('scan');
                            $from = M('user_qrcode')->where(array('qid' => $qrcode))->find();
                            $fromuser = $from['openid'];
                        }
                        $UserInfo = array(
                            'openid' => $info->openid,
                            'sex' => $info->sex,
                            'address' => $info->province . $info->city,
                            'headimgurl' => $info->headimgurl,
                            'nickname' => $info->nickname,
                            'time' => time(),
                            'from' => $fromuser
                        );
                        $UserGold = array(
                            'openid' => $info->openid,
                            'gold' => C('GOLD')
                        );
                        $result = M('user_info')->where(array('openid' => $info->openid))->find();
                        $st1r = $wechat->getRev()->getRevSceneId();
                        if (!isset($result['openid'])) {
                            M('user_info')->data($UserInfo)->add();
                            M('user_gold')->data($UserGold)->add();
                            $str = "么么哒。这位客官，小渝在这里等候你多时了哟。/:rose/:rose/:rose欢迎你关注我爱大重庆，奖励您3枚大渝币！记得每天签到抽奖哟，红包让你拿的手软哟！";
                            $wechat->text($str)->reply();
                        } else {
                            M('user_info')->where(array('openid' => $info->openid))->save(array('at' => 0));
                            $str = "么么哒。这位客官，小渝感谢您的再次关注哦。/:rose/:rose/:rose";
                            //$wechat->text($st1r)->reply();
                            $wechat->text($str)->reply();
                        }
                        break;


                    //这是点击自定义菜单事件，先获取KEY，再根据不同的KEY做不同的逻辑判断
                    case Wechat::EVENT_MENU_CLICK:
                        $key = $EventType['key'];
                        switch ($key) {

                            case "a1":
                                $str = "功能正在加班加点开发中，请稍后！小渝会一直在这里等你哟！";
                                $wechat->text($str)->reply();
                                break;

                            case "a2":
                                $str = "功能正在加班加点开发中，请稍后！小渝会一直在这里等你哟！";
                                $wechat->text($str)->reply();
                                break;

                            case "a3":
                                $str = "功能正在加班加点开发中，请稍后！小渝会一直在这里等你哟！";
                                $wechat->text($str)->reply();
                                break;

                            case "a4":
                                $str = "功能正在加班加点开发中，请稍后！小渝会一直在这里等你哟！";
                                $wechat->text($str)->reply();
                                break;

                            case "a5":
                                $str = "功能正在加班加点开发中，请稍后！小渝会一直在这里等你哟！";
                                $wechat->text($str)->reply();
                                break;


                            case "b1":
                                $news = array(
                                    "0" => array(
                                        'Title' => "抢红包活动说明:",
                                        'Description' => '',
                                        'PicUrl' => 'http://public.weirongm.com/DaChongQing/images/b_remark.jpg',
                                        'Url' => ''
                                    ),
                                    "1" => array(
                                        'Title' => "活动时间：\n即将上线，请稍后！"/*"活动时间：\n2015-8-15  6:00至2015-9-15  18:00。2015年8月15日早上8点至2015年9月15日下午18点！"*/,
                                        'Description' => '',
                                        'PicUrl' => '',
                                        'Url' => ''
                                    ),
                                    "2" => array(
                                        'Title' => "活动规则：\n点击版块“抢红包”中的“我的推广二维码”即能获取一张专属二维码，通过专属二维码关注（微信扫描或者长按二维码关注）公众号“我爱大重庆”的人数，便是你的推荐粉丝数。",
                                        'Description' => '',
                                        'PicUrl' => '',
                                        'Url' => ''
                                    ),


                                    "3" => array(
                                        'Title' => "粉丝要求：\n系统会自动判断推荐粉丝是否为重庆范围内粉丝，如果是重庆范围内粉丝，你的推荐粉丝数量加1；重复关注无效",
                                        'Description' => '',
                                        'PicUrl' => '',
                                        'Url' => ''
                                    ),




                                    "4" => array(
                                        'Title' => "奖励方式：\n当你的推荐粉丝数是20的整数倍时（例如20,40,60,80.....），系统审核通过后便会为你发放一次5元~20元的随机大红包。\n以活动结束时的推广排行榜为准，1~20名将会获得相应的红包奖励：\n第一名：200元大红包\n第二名：180元大红包\n第三名：160元大红包\n第四名~第六名：120元大红包\n第七名~第十名：100元大红包\n第十一名~第二十名：随机获得50元~100元大红包",
                                        'Description' => '',
                                        'PicUrl' => '',
                                        'Url' => ''
                                    ),
                                    "5" => array(
                                        'Title' => "注意事项：\n一旦发现作弊行为，一律视为退出本次活动。（本次活动的最终解释权归重庆微融智联科技有限公司所有）",
                                        'Description' => '',
                                        'PicUrl' => '',
                                        'Url' => ''
                                    ),
                                );
                                $wechat->news($news)->reply();
                                break;

                            case "b2":
                                $str = "系统一旦发现你的推荐粉丝中有假粉或者僵尸粉，系统将无条件取消你的抢红包资格并拉入黑名单。切记！！！\n\n排    名   推荐粉丝      昵    称\n";
                                $openid = $wechat->getRevFrom();
                                $cqdata['_string'] = 'address LIKE "%重庆%" OR location LIKE "%重庆%"';
                                $user = M('user_info')->field('`from`,count(`from`) as `order`')->where($cqdata)->group('`from`')->order('`order` desc')->limit('20')->select();
                                $i = 1;
                                $x = '';
                                foreach ($user as $k => $v) {
                                    if ($i < 10) {
                                        $i = '0' . $i;
                                    }
                                    if($user[$k]['order'] < 10){
                                        $x = "0" ;
                                    }
                                    $nick = M('user_info')->where(array('openid' => $user[$k]['from']))->find();
                                    $str .= "第" . $i . "名       " .$x.$user[$k]['order'] . "人        " .substr_cut($nick['nickname']). "\n";
                                    $i++;
                                }
                                $news = array(
                                    "0" => array(
                                        'Title' => "推广排行前20名：(正式上线后，非重庆范围内粉丝不计入成绩)",
                                        'Description' => '',
                                        'PicUrl' => 'http://public.weirongm.com/DaChongQing/images/b_list.jpg',
                                        'Url' => ''
                                    ),
                                    "1" => array(
                                        'Title' => $str,
                                        'Description' => '',
                                        'PicUrl' => '',
                                        'Url' => ''
                                    ),
                                );
                                $wechat->news($news)->reply();
                                break;

                            case "b3":
                                $list = M('user_bonus')->where(array('type' => 3))->limit('10')->select();
                                $str = "";
                                if (!empty($list)) {
                                    foreach ($list as $k => $v) {
                                        $str .= substr_cut($list[$k]['nickname']) . "在" . timeFormat($list[$k]['time']) . "领取了" . $list[$k]['bonusname'] . "\n";
                                    }
                                } else {
                                    $str .= "当前无人领取红包";
                                }
                                $news = array(
                                    "0" => array(
                                        'Title' => "抢红包实时动态：(正式上线后，非重庆范围内粉丝不计入成绩)",
                                        'Description' => '',
                                        'PicUrl' => 'http://public.weirongm.com/DaChongQing/images/b_time.jpg',
                                        'Url' => ''
                                    ),
                                    "1" => array(
                                        'Title' => $str,
                                        'Description' => '',
                                        'PicUrl' => '',
                                        'Url' => ''
                                    ),
                                );
                                $wechat->news($news)->reply();
                                break;

                            case "b4":
                                $str = "这里是推广二维码…………";
                                $wechat->text($str)->reply();
                                break;

                            case "b5":
                                $openid = $wechat->getRevFrom();
                                $from = M('user_info')->where(array('from' => $openid))->limit('10')->select();
                                $str = "关注时间           昵   称    来自\n";
                                foreach ($from as $key => $val) {
                                    $str .=  date('m-d H:i', $from[$key]['time']). "   " . substr_cut($from[$key]['nickname']) . "   ".$from[$key]['location']."\n";
                                }

                                $bonus = "";
                                $cqdata['from'] = $openid;
                                $cqdata['_string'] = 'address LIKE "%重庆%" OR location LIKE "%重庆%"';
                                $cq = M('user_info')->where($cqdata)->select();
                                if (count($cq) > 200000 ) {
                                    S('bonus',mt_rand(500,600),'60');
                                    $bonus = S('bonus');
                                    $result = QrcodeController::SendBonus($openid, $bonus);
                                    if ($result == '1') {
                                        $bonus .= "你总共拥有" . count($cq) . "重庆本地用户，是".C('FROM_USER')."的".(count($cq)/C('FROM_USER'))."倍,特此赠送你一个随机红包";
                                    }
                                } else {
                                    $bonus .= "你总共推荐了" . count($from) . "位粉丝,其中有".count($cq)."位重庆本地粉丝关注了我爱大重庆";
                                }
                                $news = array(
                                    "0" => array(
                                        'Title' => "",
                                        'Description' => '',
                                        'PicUrl' => 'http://public.weirongm.com/DaChongQing/images/b_bonus.jpg',
                                        'Url' => ''
                                    ),
                                    "1" => array(
                                        'Title' => $bonus,
                                        'Description' => '',
                                        'PicUrl' => '',
                                        'Url' => ''
                                    ),
                                    "2" => array(
                                        'Title' => $str,
                                        'Description' => '',
                                        'PicUrl' => '',
                                        'Url' => ''
                                    ),
                                );
                                $wechat->news($news)->reply();
                                break;


                            case "c1":
                                $openid = $wechat->getRevFrom();
                                $time = time();
                                $zero = date('Y-m-d', $time);
                                $h = date('H', $time);
                                $zero = strtotime($zero . " 00:00:00");
                                $SignTime = array(
                                    'sign_time' => array(
                                        'gt', $zero
                                    ),
                                    'openid' => $openid
                                );
                                $result = M('user_sign')->where($SignTime)->find();
                                if ($h < C('SIGN_START') || $h > C('SIGN_STOP')) {
                                    $str = "签到失败！老大，正所谓蜡照半笼金翡翠，麝熏微度绣芙蓉，现在这个时间，是该签到的时候吗？\n早睡早起身体好，等你睡饱了6点再来签到吧";
                                } //这里面用&&逻辑，不能用||逻辑
                                elseif ($h >= C('SIGN_START') && $h <= 19) {
                                    $img = C("MORNING_IMG");
                                    $str = "签到成功！大渝币+".C("SIGN_GOLD")."！又是辛苦忙碌的一天，加油哦";
                                } else {
                                    $img = C("NIGHT_IMG");
                                    $str = "签到成功！大渝币+".C("SIGN_GOLD")."！夜色降临，放下手机，走到户外，感受这夜的宁静吧";
                                }
                                if ($result == '' && $h >= C('SIGN_START')) {
                                    $data = array(
                                        'openid' => $openid,
                                        'sign_time' => $time
                                    );
                                    $gold = array(
                                        'openid' => $openid,
                                    );
                                    $limit = array(
                                        'sign_time' => array(
                                            'gt', $zero
                                        )
                                    );

                                    M('user_sign')->data($data)->add();
                                    M('user_gold')->where($gold)->setInc('gold', C('SIGN_GOLD'));
                                    $limit = M('user_sign')->where($limit)->order('sign_time asc')->limit('0,20')->select();
                                    $ResultUser = '';
                                    $i = 1;
                                    if (!empty($limit)) {
                                        foreach ($limit as $k => $v) {
                                            $nickname = M('user_info')->field('nickname')->where(array('openid' => $limit[$k]['openid']))->find();
                                            if ($i < 10) {
                                                $i = '0' . $i;
                                            }
                                            $ResultUser[] = array(
                                                'ranking' => "第" . $i . "名",
                                                'nickname' => $nickname['nickname'],
                                                'sign_time' => date('H:i:s', $limit[$k]['sign_time'])
                                            );
                                            $i++;
                                        }
                                        $gold = M('user_gold')->where(array('openid' => $openid))->find();
                                        $goldrank=M('user_gold')->where(array('gold'=>array("gt",$gold['gold'])))->count();
                                        $goldrank++;
                                        $rank = M('user_sign')->where('sign_time > ' . $zero . ' AND sign_time <' . time() . '')->count();
                                        $sign_list = M('user_sign')->where(array('openid' => $openid))->order('sign_time desc')->select();
                                        $sign_continue = sign_continue($sign_list);
//                                        $UserList = "今日签到完成啦，你当前拥有" . $gold['gold'] . "枚大渝币\n\n";
                                        $UserList = "排    名      签到时间      昵    称\n";
                                        foreach ($ResultUser as $k => $v) {
                                            $UserList .= "" . $ResultUser[$k]['ranking'] . "     " . $ResultUser[$k]['sign_time'] . "      " . substr_cut($ResultUser[$k]['nickname']) . " \n";
                                        }
                                        $tpl = array(
                                            "0" => array(
                                                'Title' => $str,
                                                'Description' => $str,
                                                'PicUrl' => $img,
                                                'Url' => ''
                                            ),
                                            "1" => array(
                                                'Title' => "你是大重庆第".($rank+1)."个签到的！你当前拥有" . $gold['gold'] . "枚大渝币。暂列土豪榜第".$goldrank."位！你已经连续签到" . $sign_continue . "天！请继续保持每天签到，连续签到天数越多，被红包砸中的概率也越大哟！",
                                                'Description' => '',
                                                'PicUrl' => '',
                                                'Url' => ''
                                            ),
                                            "2" => array(
                                                'Title' => $UserList,
                                                'Description' => '',
                                                'PicUrl' => '',
                                                'Url' => ''
                                            )
                                        );
                                        $wechat->news($tpl)->reply();
                                    } else {
                                        $wechat->text("恭喜你，今天你是第一个签到的")->reply();
                                    }
                                } elseif ($h < C('SIGN_START') || $h > C('SIGN_STOP')) {
                                    $wechat->text($str)->reply();
                                } else {
                                    $limit = array(
                                        'sign_time' => array(
                                            'gt', $zero
                                        )
                                    );

                                    $limit = M('user_sign')->where($limit)->order('sign_time asc')->limit('0,20')->select();
                                    $ResultUser = '';
                                    $i = 1;
                                    if (!empty($limit)) {
                                        foreach ($limit as $k => $v) {
                                            $nickname = M('user_info')->field('nickname')->where(array('openid' => $limit[$k]['openid']))->find();
                                            if ($i < 10) {
                                                $i = '0' . $i;
                                            }
                                            $ResultUser[] = array(
                                                'ranking' => "第" . $i . "名",
                                                'nickname' => $nickname['nickname'],
                                                'sign_time' => date('H:i:s', $limit[$k]['sign_time'])
                                            );
                                            $i++;
                                        }
                                        $gold = M('user_gold')->where(array('openid' => $openid))->find();
                                        $goldrank=M('user_gold')->where(array('gold'=>array("gt",$gold['gold'])))->count();
                                        $goldrank++;
                                        $stime = M('user_sign')->where(array('openid' => $openid, 'sign_time' => array('gt', $zero)))->find();
                                        $rank = M('user_sign')->where('sign_time > ' . $zero . ' AND sign_time <= ' . $stime['sign_time'] . '')->count();
                                        $sign_list = M('user_sign')->where(array('openid' => $openid))->order('sign_time desc')->select();
                                        //$sign_continue=count($sign_list);
                                        $sign_continue = sign_continue($sign_list);
                                        $UserList = "排    名      签到时间      昵    称\n";
                                        foreach ($ResultUser as $k => $v) {
                                            $UserList .= "" . $ResultUser[$k]['ranking'] . "      " . $ResultUser[$k]['sign_time'] . "     " . substr_cut($ResultUser[$k]['nickname']) . " \n";
                                        }
                                        $tpl = array(
                                            "0" => array(
                                                'Title' => "今日已签到，你当前拥有" . $gold['gold'] . "枚大渝币！暂列土豪榜第".$goldrank."位！",
                                                'Description' => '',
                                                'PicUrl' => $img,
                                                'Url' => ''
                                            ),
                                            "1" => array(
                                                'Title' => "你是大重庆第" . ($rank) . "个签到的！你已经连续签到" . $sign_continue . "天！请继续保持每天签到，连续签到天数越多，被红包砸中的概率也越大哟！",
                                                'Description' => '',
                                                'PicUrl' => '',
                                                'Url' => ''
                                            ),
                                            "2" => array(
                                                'Title' => $UserList,
                                                'Description' => '',
                                                'PicUrl' => '',
                                                'Url' => ''
                                            )
                                        );

                                        $wechat->news($tpl)->reply();
                                    }
                                }
                                break;

                            case "c2":
                                $openid = $wechat->getRevFrom();
                                $zero = date('Y-m-d', time());
                                $zero = strtotime($zero . " 00:00:00");
                                $SignTime = array(
                                    'time' => array(
                                        'gt', $zero
                                    ),
                                    'openid' => $openid
                                );
                                $SignT = array(
                                    'sign_time' => array(
                                        'gt', $zero
                                    ),
                                    'openid' => $openid
                                );
                                $gold = M('user_gold')->where(array('openid' => $openid))->find();
                                $data = array(
                                    'type' => array(
                                        'neq','999'
                                    )
                                );
                                $BonusList = M('user_bonus')->where($data)->limit('10')->order('time desc')->select();
                                $bonus = M('user_bonus')->where($SignTime)->find();
                                $isSign = M('user_sign')->where($SignT)->find();
                                $info = M('user_info')->where(array('openid'=>$openid))->find();
                                $record = "          实时中奖动态\n\n";
                                foreach ($BonusList as $key => $value) {
                                    $nickname = old_cut($value["nickname"]);
                                    $time = timeFormat($value["time"]);
                                    $bonusname = $value["bonusname"];


                                    $record .= $nickname . "在" . $time . "中了" . $bonusname . "\n";

                                }
                                //今天已经抽奖了
                                if (count($bonus) >= C('LUCKY_DRAW')) {
                                    $title = '你今天已经抽过奖了哦，明天记得再来哟';
                                    $url = '';
                                } //还没签到
                                elseif (empty($isSign)) {
                                    $title = '亲，先签到才能抽奖哦';
                                    $url = '';
                                } //金币不足
                                elseif ($gold['gold'] < C('GOLD_DEC')) {
                                    $title = '你的大渝币已经不够抽奖了，快去赚取大渝币吧';
                                    $url = '';
                                } 
                                elseif(intval(date("H"))<8) {
                                    $title = '抽奖系统开放时间是早上8点到24点，请在8点以后再来抽奖！';
                                    $url ='';
                                }/*elseif($info['location'] != "重庆市"){
                                    $title = '检测到你不是重庆市本地用户哦，天天来抽奖只能是重庆本地用户才行哦';
                                    $url = '';
                                }*/
                                else
                                {
//                                   $title = '亲，由于红包接口已经超出微信频率限制，暂时不能抽奖哟，稍后恢复！';
                                 $title = '你今天还有1次抽奖机会，点击这里抽奖哦';
                                 $url = C("CHOUJIANG_URL");
//                                   $url="";
                                }



                                $tpl = array(
                                    "0" => array(
                                        'Title' => $title,
                                        'Description' => '',
                                        'PicUrl' => C("CHOUJIANG_IMG"),
                                        'Url' => $url
                                    ),
                                    "1" => array(
                                        'Title' => '你当前拥有' . $gold['gold'] . '枚大渝币，抽奖一次花费3个大渝币哦',
                                        'Description' => '',
                                        'PicUrl' => '',
                                        'Url' => $url
                                    ),


                                    "2" => array(
                                        'Title' => $record,
                                        'Description' => '',
                                        'PicUrl' => '',
                                        'Url' => ''
                                    ),


                                );


                                $wechat->news($tpl)->reply();
                                break;

                            case "c3":
                                $str = "🚀来啦来啦!" . "么么哒。这位客官,小渝来了哈，你有啥子想对我们说呢？小渝正拿着本本儿记起哈!";
                                $wechat->text($str)->reply();
                                break;

                            case "c4":
                                $openid = $wechat->getRevFrom();
                                $gold = M('user_gold')->where(array('openid' => $openid))->find();
                                $str = "你目前的大渝币有" . $gold["gold"] . "枚，我们稍后会上线大渝币兑换商城，各种限量大礼包，免费门票，免费电影票，优惠券，这些通通都兑换给您！大渝币速速攒起来哟！";

                                $str1="              土豪排行榜\n\n";
                                $userlist=M("user_gold")->order('gold desc')->limit(20)->select();
                                $i = 1;
                                $ResultUser=array();
                                if (!empty($userlist)) {
foreach ($userlist as $k => $v) {
$nickname = M('user_info')->field('nickname')->where(array('openid' => $userlist[$k]['openid']))->find();
if ($i < 10) {
$i = '0' . $i;
}
$ResultUser[] = array(
'ranking' => "第" . $i . "名",
'nickname' => $nickname['nickname'],
'gold' =>  $userlist[$k]['gold']
);
$i++;
}
}

                    $str1.= "排    名      大渝币       微信名   \n";
                         foreach ($ResultUser as $k => $v)
                     {
                     $str1.= "" . $ResultUser[$k]['ranking'] . "      " . $ResultUser[$k]['gold'] . "             " . substr_cut($ResultUser[$k]['nickname']) . " \n";
                     }

                                $tpl = array(
                                    "0" => array(
                                        'Title' => '',
                                        'Description' => '',
                                        'PicUrl' => C("DUIHUAN_IMG"),
                                        'Url' => $url
                                    ),
                                    "1" => array(
                                        'Title' => $str,
                                        'Description' => '',
                                        'PicUrl' => '',
                                        'Url' => ''
                                    ),
                                    "2" => array(
                                        'Title' => $str1,
                                        'Description' => '',
                                        'PicUrl' => '',
                                        'Url' => ''
                                    ),

                                );


                                $wechat->news($tpl)->reply();
                                break;

                            case "c5":
                                $str = "公司创始人：安强， 2014年毕业于重庆大学工业工程系。前星际争霸2半职业选手。大四（2013.9~2014.7）开始零基础学编程，一年内写了超过15万行代码，从大学的4200元学费里拿出了3000元开始了自己的创业之旅，靠专业的微信开发和独特的销售技巧赚了人生第一个70万。一个对编程和营销都相当爱好的创业者。大四下半学期参股两家公司，毕业半年后自己控股的公司成立。（公司成立于重庆江北最繁华的商圈观音桥的核心地带）2015年的目标是再成立三家公司。
人称：营销狂魔，编程机器。";
                                $wechat->text($str)->reply();
                                break;

                            default:
                                $str = "功能正在加班加点开发中，请稍后！小渝会一直在这里等你哟！";
                                $wechat->text($str)->reply();
                                break;


                        }


                }


        }

    }

    /**
     * 从微信服务器拉取用户信息并存入数据库
     * 数据量大的时候绝壁会超时卡死慎用
     * 如此强大的魔法已经被我封印
     * 擅自释放这股强大的力量
     * 除了银河计算，没人可以收服
     * public function GetWxUser(){
     *     $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".AccessToken()."";
     *    $user = file_get_contents($url);
     *   $user = json_decode($user);
     *   $openid = $user->data->openid;
     *   $userinfo = '';
     *   foreach($openid as $k => $v){
     *       $info = json_decode(GetUserInfo($v));
     *        $userinfo[] = array(
     *           'openid' => $v,
     *           'sex' => $info->sex,
     *            'address' => $info->province.$info->city,
     *            'headimgurl' => $info->headimgurl,
     *            'nickname' => $info->nickname,
     *            'time' => time()
     *        );
     *        $userinfo[] = array(
     *            'openid' => $v,
     *            'gold' => '3'
     *        );
     *    }
     *    foreach($userinfo as $k => $v){
     *        $result = M('user_gold')->where(array('openid'=>$userinfo[$k]['openid']))->find();
     *        if($result == ''){
     *            M('user_gold')->data($userinfo[$k])->add();
     *            echo "<span style='border-bottom:2px solid red'>数据".$userinfo[$k]['nickname']."已经存入</span>";
     *        }else{
     *            echo "<span style='border-bottom:2px solid red'>数据".$userinfo[$k]['nickname']."已经存在</span>";
     *        }
     *    }
     * }
     */

}


?>